import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { MemberInfoEntity } from '../../entity/info';
import { MemberInfoService } from '../../service/info';
import { MemberGradeEntity } from '../../entity/grade';

/**
 * 会员信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: MemberInfoEntity,
  service: MemberInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name', 'a.nickName', 'a.phone'],
    fieldEq: ['a.gender', 'a.status', 'a.gradeId'],
    addOrderBy: {
      createTime: 'desc',
    },
    select: ['a.*', 'b.gradeName'],
    join: [
      {
        entity: MemberGradeEntity,
        alias: 'b',
        condition: 'a.gradeId = b.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const { minScore, maxScore, registerTime } = ctx.request.body;
      const condition = [];
      if (minScore !== undefined) {
        condition.push(['a.score >= :minScore', { minScore }]);
      }
      if (maxScore !== undefined) {
        condition.push(['a.score <= :maxScore', { maxScore }]);
      }
      if (registerTime && registerTime.length === 2) {
        condition.push([
          'a.registerTime BETWEEN :start AND :end',
          { start: registerTime[0], end: registerTime[1] },
        ]);
      }
      return condition;
    },
  },
})
export class AdminMemberInfoController extends BaseController {
  @Inject()
  memberInfoService: MemberInfoService;
}
